<?php
/*
Plugin Name: dKret todo widget
Description: A todo list widget (needs plugin to show up).
Author: J. Kretzschmar
Version: 1.0
Author URI: http://diekretzschmars.de
*/

if (!function_exists('ourtodolist_exists'))
	return;

function widget_dkret_todo($args) {
	extract($args);
	$options = get_option('widget_dkret_todo');
	$title = empty($options['title']) ? __('Todo Tasks','dkret') : $options['title'];	
 	echo "\t\t\t$before_widget\n";
	echo "\t\t\t\t$before_title".$title.$after_title;
	echo "\t\t\t\t<ul>\n";
ourtodolist(intval($options['todo']), false, false);
ourtodolist_completed(intval($options['finished']), false, false);
	echo "\n\t\t\t\t</ul>";
	echo "\n\t\t\t$after_widget";
}

// Admin section
function widget_control_dkret_todo() {
 	global $wpdb;
 	$options = get_option('widget_dkret_todo');
    		
	if ( $_POST['dkret-submit'] ) {

		$options['title'] = strip_tags(stripslashes($_POST['dkret-todo-title']));
		$options['todo'] = trim($_POST['dkret-todo-todo']);
		$options['finished'] = trim($_POST['dkret-todo-finished']);
		update_option('widget_dkret_todo', $options);
	}
	
	$title = htmlspecialchars($options['title'], ENT_QUOTES);
	$todo = $options['todo'];
	$finished = $options['finished'];

	// The Box content
	echo '<p style="text-align:right;"><label for="dkret-todo-title" />' . __('Title:') . ' <input style="width: 200px;" id="dkret-todo-title" name="dkret-todo-title" type="text" value="'.$title.'" /></p>';
	echo '<p style="text-align:right;"><label for="dkret-todo-todo" />' . __('Todo Tasks:', 'dkret') . ' <input size="2" id="dkret-todo-todo" name="dkret-todo-todo" type="text" value="'.$todo.'" /></p>';
	echo '<p style="text-align:right;"><label for="dkret-todo-finished" />' . __('Finished Tasks:', 'dkret') . ' <input size="2" id="dkret-todo-finished" name="dkret-todo-finished" type="text" value="'.$finished.'" /></p>';
	echo '<input type="hidden" id="dkret-submit" name="dkret-submit" value="1" />';
}

$options = get_option('widget_dkret_todo');
if ( !is_array($options) )
	$options = array('todo'=>'3','finished'=>'3',);
	update_option('widget_dkret_todo', $options);

$widget_ops = array('classname' => 'widget_dkret_todo', 'description' => __( "Display your todo list (dkret)", 'dkret') );
$control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
wp_register_sidebar_widget('todo', __('Todo','dkret'), 'widget_dkret_todo', $widget_ops);
wp_register_widget_control('todo', __('Todo','dkret'), 'widget_control_dkret_todo', $control_ops);
?>
